﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using Guest_room.BLL;
namespace Guest_room
{
    public partial class roapfo : Form
    {
        public roapfo()
        {
            InitializeComponent();
            pictureBox1.AllowDrop = true;
            pictureBox1.DragEnter += pictureBox1_DragEnter;
            pictureBox1.DragDrop += pictureBox1_DragDrop;
        }
        //创建适配器对象
        SqlDataAdapter dacustomer, daroom;
        DataSet ds = new DataSet();//适配器对象
        public static string  maxid;
        void init()
        {
            DB.GetCn();
            string str = "select * from 客户基本信息表";
            string srr = "select * from 用户购物车表";
            dacustomer = new SqlDataAdapter(str, DB.cn);
            daroom = new SqlDataAdapter(srr, DB.cn);
            dacustomer.Fill(ds, "pro_info");
            daroom.Fill(ds, "room_info");
            DB.cn.Close();
        }
        void showAll()
        {
            textBox5.Text = roominfo.room_id;
            //textBox5不可编辑
            textBox5.ReadOnly = true;
            //textBox8不可编辑
            textBox8.ReadOnly = true;
            //textBox7不可编辑
            textBox7.ReadOnly = true;
            //给客户编号maxid（KH008）加1
            int i = Convert.ToInt32(maxid.Substring(2, 3));
            i++;
            maxid = maxid.Substring(0, 2) + i.ToString().PadLeft(3, '0');
            textBox1.Text = maxid;
            //textBox1不可编辑
            textBox1.ReadOnly = true;
        }
        //获取客户编号最大值
        void getmaxid()
        {
            DB.GetCn();
            string str = "select max(客户编号) from 客户基本信息表";
            SqlCommand cmd = new SqlCommand(str, DB.cn);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                maxid = dr[0].ToString();
            }
            DB.cn.Close();
        }
        //获取客房最大容纳人数
        private void roapfo_Load(object sender, EventArgs e)//窗体加载函数
        {
            init();
            getmaxid();
            showAll();//显示全部
            // label11.Text = roin.maxnum + "人";
            textBox8.Text = roominfo.room_price;
            // textBox7.Text = (Convert.ToInt32(roominfo.room_price)*Convert.ToInt32(numericUpDown1)).ToString();
            numericUpDown1_ValueChanged(sender, e);
            label14.Text = "当前用户："+(Login.username ?? Home.username);
        }
        private string savedImagePath; // 保存图片的路径
        GuestRoomService guestRoomService = new GuestRoomService();
        private void button1_Click(object sender, EventArgs e)// 提交验证
        {
            try
            {
                string savedImagePath = ""; // 获取实际的图像路径
                guestRoomService.ProcessGuestRoom(ds, dacustomer, daroom,
                                                  textBox1.Text, textBox2.Text, textBox3.Text,
                                                  textBox4.Text, textBox5.Text, textBox8.Text,
                                                  Convert.ToInt32(numericUpDown2.Text),
                                                  dateTimePicker1.Text, Convert.ToInt32(numericUpDown1.Text),
                                                  Convert.ToInt32(textBox7.Text),
                                                  Login.userid ?? Home.userid, savedImagePath);

                MessageBox.Show("提交成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            this.Dispose();
            roominfo roin = new roominfo();
            roin.ShowDialog();
        }


        //只要numericUpDown1的值发生变化，就会触发该事件
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            textBox7.Text = (Convert.ToInt32(textBox8.Text) * Convert.ToInt32(numericUpDown1.Value)).ToString();
        }

        private void pictureBox1_Click(object sender, EventArgs e)  //身份证照片
        {
        }
        private void pictureBox1_DragEnter(object sender, DragEventArgs e)  //身份证照片
        {

            if (e.Data.GetDataPresent(DataFormats.FileDrop))    //判断拖来的是否是文件
            {
                e.Effect = DragDropEffects.Copy;    //是则将拖动源中的数据拷贝到目标中
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
        int index = 1;
        private void pictureBox1_DragDrop(object sender, DragEventArgs e)   //模拟刷身份证 
        {

            string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);    //获得路径
            if (fileNames.Length > 0)   //如果拖来的是文件
            {
                try
                {
                    pictureBox1.Image = Image.FromFile(fileNames[0]);   //将图片赋值给picturebox
                    savedImagePath = fileNames[0]; // 保存图片路径，稍后在提交时使用
                    textBox2.Text = "张" + index.ToString();
                    textBox3.Text = "1234567890123456" + index + "8";
                    textBox4.Text = "12345678901";
                    numericUpDown1.Text = index + "";
                    numericUpDown2.Text = index + "";
                    index++;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("加载图片出错，错误信息：" + ex.Message);   //弹出异常信息
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)//返回
        {
            this.Dispose();
            roominfo roin = new roominfo();
            roin.ShowDialog();
        }

        // int index = 1;
        private void button6_Click(object sender, EventArgs e)//填充测试数据
        {
            textBox2.Text = "张" + index.ToString();
            textBox3.Text = "1234567890123456" + index + "8";
            textBox4.Text = "12345678901";
            numericUpDown1.Text = index + "";
            numericUpDown2.Text = index + "";
            textBox7.Text = "100";
            index++;
        }
    }
}
